package com.shi.yoyoshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shi.yoyoshop.bean.User;
import com.shi.yoyoshop.mapper.UserMapper;
import com.shi.yoyoshop.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 20556
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-01-05 13:30:41
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User getUserById(Integer userId) {
        return userMapper.selectOne(new LambdaQueryWrapper<User>()
                .select(User::getId, User::getUsername, User::getPassword, User::getName, User::getPhone, User::getAddress, User::getStatus)
                .eq(User::getId, userId)
                .eq(User::getStatus, 1));
    }

    @Override
    public User getUserByUsername(String username) {
        return userMapper.selectOne(new LambdaQueryWrapper<User>()
                .select(User::getId, User::getUsername, User::getPassword, User::getName, User::getPhone, User::getAddress, User::getStatus)
                .eq(User::getUsername, username)
                .eq(User::getStatus, 1));
    }

    @Override
    public User getUserByPhone(String phone) {
        return userMapper.selectOne(new LambdaQueryWrapper<User>()
                .select(User::getId, User::getUsername, User::getPassword, User::getName, User::getPhone, User::getAddress, User::getStatus)
                .eq(User::getPhone, phone)
                .eq(User::getStatus, 1));
    }

    @Override
    public void resetPasswordByUserId(User user) {
        userMapper.update(new User(),new LambdaUpdateWrapper<User>()
                .set(User::getPassword, user.getPassword())
                .eq(User::getId, user.getId()));
    }

    @Override
    public IPage<User> getUserListWithPage(Page<User> userPage) {
        return userMapper.selectPage(userPage, new LambdaQueryWrapper<User>()
                .select(User::getId, User::getUsername, User::getPassword, User::getName, User::getPhone, User::getAddress, User::getStatus)
                .eq(User::getStatus, 1));
    }
}




